/*
  Copyright (c) 2000,2001 Al Sutton (al@alsutton.com)
  All rights reserved.
  Redistribution and use in source and binary forms, with or without modification, are permitted
  provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions
  and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of
  conditions and the following disclaimer in the documentation and/or other materials provided with
  the distribution.

  Neither the name of Al Sutton nor the names of its contributors may be used to endorse or promote
  products derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.alsutton.jabber;

/**
 * The stream to a jabber server.
 */

import java.io.*;

public class JabberStream {
  /**
   * The Output stream to the server.
   */

  private OutputStream outStream;

  /**
   * The input stream from the server.
   */

  private InputStream inpStream;

  /**
   * The dispatcher thread.
   */

  private JabberDataBlockDispatcher dispatcher;
  
  private StreamThread streamThread;
  
  

  /**
   * Constructor. Connects to the server and sends the jabber welcome message.
   *
   * @param connectorInterface The connector which establishes the socket for
   * the connection
   */

  public JabberStream( ConnectorInterface connectorInterface )
    throws IOException
  {
    this( connectorInterface, null );
  }

  /**
   * Constructor. Connects to the server and sends the jabber welcome message.
   *
   * @param connectorInterface The connector which establishes the socket for
   * the connection
   */

  public JabberStream( ConnectorInterface connectorInterface, JabberListener theListener )
    throws IOException
  {
    dispatcher = new JabberDataBlockDispatcher();
    if( theListener != null )
    {
      setJabberListener( theListener );
    }

    inpStream = connectorInterface.openInputStream();
    outStream = connectorInterface.openOutputStream();

    StringBuffer streamInitiator = new StringBuffer( "<stream:stream to=\"" );
    String hostname = connectorInterface.getHostname();
    streamInitiator.append( hostname );
    streamInitiator.append( "\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">" );
    outStream.write( streamInitiator.toString().getBytes() );
    outStream.flush();

    startListeningThread();
    /*
	streamInitiator.append(hostname);
	streamInitiator.append("' from='");
	streamInitiator.append(username + "@" + hostname);
	streamInitiator.append("' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>");
    send(streamInitiator.toString());
    startListeningThread();*/
  }
  
  /**
   * Method of sending data to the server.
   *
   * @param data The data to send.
   */

  public void send( byte[] data ) throws IOException
  {
    outStream.write( data );
    outStream.flush();
  }

  /**
   * Method of sending data to the server.
   *
   * @param The data to send to the server.
   */

  public void send( String data ) throws IOException
  {
    send( data.getBytes() );
  }

  /**
   * Method of sending a Jabber datablock to the server.
   *
   * @param block The data block to send to the server.
   */

  public void send( JabberDataBlock block ) throws IOException
  {
    send( block.getBytes() );
  }

  /**
   * Set the listener to this stream.
   */

  public void setJabberListener( JabberListener listener )
  {
    dispatcher.setJabberListener( listener );
  }
  
  /**
   * Method to close the connection to the server and tell the listener
   * that the connection has been terminated.
   */

  public void close()
  {
    dispatcher.setJabberListener( null );
    try
    {
      send( "</stream:stream>" );
      inpStream.close();
      outStream.close();
    }
    catch( IOException e )
    {
      // Ignore an IO Exceptions because they mean that the stream is
      // unavailable, which is irrelevant.
    }
    finally
    {
      dispatcher.halt();
    }
  }
  
  /**
   * Method to close the connection to the server (stream only) and tell the listener
   * that the connection has been terminated.
   */

  public void closeStream()
  {
    dispatcher.setJabberListener( null );
    try
    {
      inpStream.close();
      outStream.close();
    }
    catch( IOException e )
    {
      // Ignore an IO Exceptions because they mean that the stream is
      // unavailable, which is irrelevant.
    }
    finally
    {
      dispatcher.halt();
    }
  }
  
  public void startListeningThread() {
      streamThread = new StreamThread(inpStream, dispatcher);
      Thread newThread = new Thread( streamThread );
      newThread.start();
  }
}